﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Truron.Purchase.Interface;
using Truron.Purchase.Api.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Truron.Purchase.Model;
using Newtonsoft.Json;
using Truron.Purchase.Utility;

namespace Truron.Purchase.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Purchase_LeftGroupController : ControllerBase
    {
        private readonly IPurchase_LeftGroupService _purchase_LeftGroupService;
        public Purchase_LeftGroupController(IPurchase_LeftGroupService Purchase_LeftGroupService)
        {
            _purchase_LeftGroupService = Purchase_LeftGroupService;
        }
        public class GetLeftGroupParam
        {
            public string key { get; set; }
        }
        public IActionResult GetLeftGroup(GetLeftGroupParam param)
        {
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            Purchase_LeftGroup entity = _purchase_LeftGroupService.GetLeftGroup(param.key, userToken);
            string result = "";
            if (entity!=null)
            {
                result = entity.GroupJson;
            }
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = result,code=0 }));
        }
        public class SaveParam
        {
            public string key { get; set; }
            public string GroupJson { get; set; }
        }
        public IActionResult Save(SaveParam param)
        {
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            Purchase_LeftGroup entity = new Purchase_LeftGroup();
            entity.UserId = userToken.UserId;
            entity.Key = param.key;
            entity.GroupJson = param.GroupJson;
            _purchase_LeftGroupService.SaveLeftGroup(entity, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0 }));
        }
    }
}
